<%@ Page Language="C#" AutoEventWireup="true" Inherits="Documentation_CRUD_EntityControls" Codebehind="EntityControls.aspx.cs" %>

<%@ Register Assembly="Pampa.CRUD" Namespace="Pampa.CRUD.UI.CustomControls" TagPrefix="cc2" %>

<%@ Register Assembly="Pampa.WebControls" Namespace="Pampa.WebControls" TagPrefix="cc1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <link href="../../Css/Layout.css" rel="stylesheet" type="text/css"/>   
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <span class="title">Entity Controls</span>
        <br />
        <br />
        There are some special controls in Pampa.CRUD (EntityCombo, EntityCheckList and EntitySelector) which allows us to list all objects of a particular class in order to choose one.
        These controls are used in the EntityEdit component and works just with entities.
        An entity is understood as all classes of the Model which implement Pampa.CRUD.Core.IActiveRecord (see <a href="Entities.aspx">Entities</a>).
        <br />
        <br />
        <br />
        
        <span class="subtitle">IIDDescription</span>
        <br />
        <br />            
        All classes that we want to show in an Entity Control has to implement also this interface. 
        It allow the control to ask for an id and a description for each objects of this class.
        <br />
        <br />            

<pre class="codeBlock">
<span class="keyword">public interface</span> <span class="className">IIdDescription</span>
{ 
   <span class="keyword">string</span> Id { <span class="keyword">get</span>; }
   <span class="keyword">string</span> Description { <span class="keyword">get</span>; }
}
</pre>
        
        <br />     
        <span class="subtitle">Country Class</span>
        <br />
        <br />
        Here is a class called Country which we will use bellow to see entity controls working.
        Again, statics methods necessary to entity controls working can be easily implemented using <a href="http://www.castleproject.org/activerecord/index.html">Castle.ActiveRecord</a>
        but we could implement those however we want. I also present a class called "AbstractEntity" which could has all common functionality for entity classes in our Model.
        <br />
        <br />     
        <br />


<pre class="codeBlock">
[ActiveRecord]
<span class="keyword">public class</span> <span class="className">Country</span> : <span class="className">AbstractEntity&lt;Country&gt;</span>, <span class="className">IIdDescription</span>
{
   <span class="keyword">private int</span> id;
   <span class="keyword">private string</span> description;

   <span class="keyword">public</span> Country()
   { 
   }

   [PrimaryKey]
   <span class="keyword">public virtual int</span> ID
   {
      <span class="keyword">get</span> { <span class="keyword">return</span> id;}
      <span class="keyword">set</span> { id = value;}
   }

   [Property]
   <span class="keyword">public virtual string</span> Description
   {
      <span class="keyword">get</span> { <span class="keyword">return</span> description; }
      <span class="keyword">set</span> { description = value; }
   }

   <span class="keyword">string</span> Pampa.CRUD.Core.<span class="className">IIdDescription</span>.Id
   {
      <span class="keyword">get</span> { <span class="keyword">return</span> this.ID.ToString(); }
   }

   <span class="keyword">string</span> Pampa.CRUD.Core.<span class="className">IIdDescription</span>.Description
   {
      <span class="keyword">get</span> { <span class="keyword">return</span> this.Description; }
   }
   
   ...
   
}
</pre>

<pre class="codeBlock">
<span class="keyword">public class</span> <span class="className">AbstractEntity&lt;T&gt;</span> : <span class="className">ActiveRecordBase</span>, <span class="className">IActiveRecord</span>
{

   ...

   <span class="keyword">public static</span> T Find(<span class="keyword">object</span> id)
   {
      <span class="keyword">return</span> (T)FindByPrimaryKey(typeof(T), id);
   }

   <span class="keyword">public static</span> T[] FindAll(<span class="keyword">string</span> orderBy)
   {
      Order[] orders = <span class="keyword">get</span>Orders(orderBy);
      <span class="keyword">return</span> (T[])ActiveRecordBase.FindAll(typeof(T), orders);
   }

   <span class="keyword">public static</span> T[] FindAllByPropertyLike(<span class="keyword">string</span> descriptionValue, <span class="keyword">int</span> maxResults)
   {
      Order[] order = new Order[] { Order.Asc("Description") };
      <span class="keyword">return</span> (T[])ActiveRecordBase.SlicedFindAll(typeof(T), 0, maxResults, order, Expression.Like("Description", descriptionValue, MatchMode.Anywhere));
   }

   <span class="keyword">protected static</span> <span class="className">Order</span>[] getOrders(<span class="keyword">string</span> orderBy)
   {
      <span class="keyword">string</span>[] arrOrders = orderBy.Split(',');
      <span class="className">Order</span>[] orders = <span class="keyword">new</span> <span class="className">Order</span>[arrOrders.Length];

      <span class="keyword">for</span> (<span class="keyword">int</span> i = 0; i < arrOrders.Length; i++)
      {
         orders[i] = <span class="className">Order</span>.Asc(arrOrders[i].Trim());
      }

      <span class="keyword">return</span> orders;
   }
        
   ...
        
}
</pre>

                
        <br />
        <br />     
        <span class="subtitle">Entity Combo</span>
        <br />
        <br />      
        This control shows all elements from a class in a drop down list. 
        Classes intended to show itself in an Entity Combo has to implement the static method: <br />
<pre>
<span class="className">MyClass</span>[] FindAll(<span class="keyword">string</span> orderBy)
</pre>
        where MyClass[] is an array of all objects of type MyClass we want to see listed 
        and orderBy is a quote separated list of fields. 
        <br/>
        <br/>
        Here we can see a list of countries alphabetically ordered. Just setting the properties:<br />
<pre>
EntityName = "Demo.Model.Country, Demo.Model"
OrderBy = "Description"
</pre>
        <cc2:EntityComboList ID="EntityComboList1" runat="server" EntityName="Demo.Model.Country, Demo.Model" OrderBy="Description"
            Height="25px" Width="213px" />
        <br />          
        <br />          


        <span class="subtitle">Entity Selector</span>
        <br />
        <br />        
        This control allows us to find and element of a class when all the list is too large to be shown in a standard drop down list. 
        Classes intended to show itself in an Entity Selector has to implement the static method:
<pre>
<span class="className">MyClass</span>[] FindAllByPropertyLike(<span class="keyword">string</span> descriptionValue, <span class="keyword">int</span> maxResults)
</pre>
        where MyClass[] is an array of all objects of type MyClass we want to see listed, 
        descriptionValue is the text inserted in the textbox and maxResults is the maximum size of the list. 
        <br/>
        <br/>
        Here we can see a selector of countries just setting the property:
<pre>
EntityName = "Demo.Model.Country, Demo.Model"
</pre>
        <cc2:EntitySelector ID="EntitySelector1" runat="server" EntityName="Demo.Model.Country, Demo.Model"
            Height="25px" Width="214px" />
        <br />
        <br />  
        
        
        <span class="subtitle">Entity Check List</span>
        <br />
        <br />        
        This control shows all elements from a class in a check list. 
        Class should be implement same FindAll method like with an Entity Combo.
        <br />
        <br/>        
        Here we can see a check list of countries alphabetically ordered. Just setting the properties:
<pre>
EntityName = "Demo.Model.Country, Demo.Model"
OrderBy = "Description"
</pre>		
        <cc2:EntityCheckList ID="EntityCheckList1" runat="server" EntityName="Demo.Model.Country, Demo.Model" OrderBy="Description"
            Height="24px" Width="215px" />
        <br />
        <br />          
        <br />          
       
    </div>
    </form>
</body>
</html>
